//
//  WmcCloudAllPhotoFooterView.swift
//  YPWatermarkCamera
//
//  Created by 周诗松 on 2022/5/13.
//

import UIKit
import YPBedrock

class WmcCloudAllPhotoFooterView: UICollectionReusableView {
    // Lifecycle

    override init(frame: CGRect) {
        super.init(frame: frame)
        makeUI()
        bindEvent()
    }

    @available(*, unavailable)
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    // Internal

    func bindModel(_ model: WmcMediaPhotoPlaceModel) {
        self.model = model

        if model.address.isEmpty {
            addressLabel.text = "未获取到地点信息"
            arrowImageView.isHidden = true
        } else {
            addressLabel.text = model.address
            arrowImageView.isHidden = false
        }
    }

    // Private

    private lazy var locationImageView: UIImageView = {
        let imageView = UIImageView(image: "location_icon".wmc_image)
        imageView.yp.set(tintColor: UIColor.wmc_grayH5_606066)
        return imageView
    }()

    private lazy var addressLabel: UILabel = {
        let label = UILabel()
        label.textColor = .wmc_grayH5_606066
        label.font = UIFont.systemFont(ofSize: 14, weight: .medium)
        return label
    }()

    private lazy var arrowImageView: UIImageView = {
        let imageView = UIImageView(image: "arrow_right".wmc_image)
        imageView.yp.set(tintColor: UIColor.wmc_grayH5_606066)
        return imageView
    }()

    private var model: WmcMediaPhotoPlaceModel?

    private func makeUI() {
        self.backgroundColor = .wmc_white_FFFFFF

        addSubview(locationImageView)
        addSubview(addressLabel)
        addSubview(arrowImageView)

        locationImageView.snp.makeConstraints { make in
            make.left.equalTo(16)
            make.centerY.equalToSuperview()
            make.width.height.equalTo(18)
        }

        let maxWidth = bounds.width - 48
        addressLabel.snp.makeConstraints { make in
            make.left.equalTo(locationImageView.snp.right).offset(1)
            make.centerY.equalToSuperview()
            make.width.lessThanOrEqualTo(maxWidth)
        }

        arrowImageView.snp.makeConstraints { make in
            make.left.equalTo(addressLabel.snp.right).offset(1)
            make.centerY.equalToSuperview()
            make.width.height.equalTo(12)
        }
    }

    private func bindEvent() {
        let tap = UITapGestureRecognizer(target: self, action: #selector(tapAction))
        isUserInteractionEnabled = true
        addGestureRecognizer(tap)
    }

    @objc
    private func tapAction() {
        guard let model = model, !model.address.isEmpty else {
            return WmcHudManager.toast("未获取到地点信息")
        }

        let maps = WmcMapManager.canOpenMaps()

        if maps.isEmpty {
            let alert = WmcAlertView()
            alert.title = "温馨提示"
            alert.msg = "请先安装地图app"
            alert.style = .confirm(title: "确定")
            alert.show()
            return
        }

        let selectMapAlertView = WmcSelectMapAlertView()
        selectMapAlertView.maps = maps
        addSubview(selectMapAlertView)

        selectMapAlertView.clickClosure = { map in
            WmcMapManager.openMap(latitude: model.latitude, longitute: model.longitude, name: model.address, mapType: map)
            YPApp.CurrentVC?.dismiss(animated: true)
        }

        let presentVC = WmcPresentVC(config: .init(
            style: .sheet(),
            contentView: selectMapAlertView,
            contentLayoutBlock: { make in
                let count = maps.count <= 2 ? 3 : maps.count
                let height = 44.wmc_autoVer + CGFloat(52 * count) + Wmc_safeAreaBottom
                    make.left.right.bottom.equalToSuperview()
                    make.height.equalTo(height)
            }))
        presentVC.show()
    }
}
